package moltencore.network.protocol;

import moltencore.game.bubble.Bubble;
import moltencore.game.bubble.Type;
import spark.math.Vector2;
import spark.network.Util;

import java.nio.ByteBuffer;

public class GridBubbleData {
	private short bubbleId;
	private static final int BUBBLE_ID_LENGTH = Short.SIZE / 8;
	private byte gridPositionX;
	private static final int GRID_POSITION_X_LENGTH = Byte.SIZE / 8;
	private byte gridPositionY;
	private static final int GRID_POSITION_Y_LENGTH = Byte.SIZE / 8;
	private Type type;
	private static final int TYPE_LENGTH = Byte.SIZE / 8;

	public static final int SIZE =
			BUBBLE_ID_LENGTH +
			GRID_POSITION_X_LENGTH +
			GRID_POSITION_Y_LENGTH +
			TYPE_LENGTH;

	public GridBubbleData() {
	}

	public GridBubbleData(Bubble bubble) {
		this.bubbleId = bubble.getBubbleId();
		this.gridPositionX = (byte)bubble.getGridPosition().x;
		this.gridPositionY = (byte)bubble.getGridPosition().y;
		this.type = bubble.getType();
	}

	public short getBubbleId() {
		return bubbleId;
	}

	public void setBubbleId(short bubbleId) {
		this.bubbleId = bubbleId;
	}

	public byte getGridPositionX() {
		return gridPositionX;
	}

	public void setGridPositionX(byte gridPositionX) {
		this.gridPositionX = gridPositionX;
	}

	public byte getGridPositionY() {
		return gridPositionY;
	}

	public void setGridPositionY(byte gridPositionY) {
		this.gridPositionY = gridPositionY;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public void getBytes(byte[] buffer, int offset) {
		ByteBuffer bb = ByteBuffer.wrap(buffer, offset, buffer.length - offset);
		bb.putShort(bubbleId);
		bb.put(gridPositionX);
		bb.put(gridPositionY);
		bb.put(type.toByte());
	}

	public void fill(byte[] buffer, int offset) {
		ByteBuffer bb = ByteBuffer.wrap(buffer, offset, buffer.length - offset);
		bubbleId = bb.getShort();
		gridPositionX = bb.get();
		gridPositionY = bb.get();
		type = Type.fromByte(bb.get());
	}

	public int getSize() {
		return SIZE;
	}
}